package net.zdreamer.shunyi.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import net.zdreamer.shunyi.ui.HomeActivity;
import net.zdreamer.shunyi.R;

import java.util.ArrayList;
import java.util.List;

/** 
 */
public class HomepageViewPagerAdapter extends PagerAdapter {

	// 界面列表
	List<String> ImageURL; /* 要加载的图片地址 */
	private List<View> views;

    private Activity mActivity;
	private static final String SHAREDPREFERENCES_NAME = "first_pref";

	public HomepageViewPagerAdapter(Activity activityt) {
		//this.activity = fragment.getActivity();
        this.mActivity=activityt;

		/*
		 * test code
		 */
		views = new ArrayList<View>();
		ImageView img1 = new ImageView(mActivity);
		img1.setBackgroundResource(R.drawable.zhuyeguanggao1);
		views.add(img1);

		ImageView img2 = new ImageView(mActivity);
		img2.setBackgroundResource(R.drawable.zhuyeguanggao2);
		views.add(img2);

		ImageView img3 = new ImageView(mActivity);
		img3.setBackgroundResource(R.drawable.zhuyeguanggao3);
		views.add(img3);

		ImageView img4 = new ImageView(mActivity);
		img4.setBackgroundResource(R.drawable.zhuyeguanggao4);
		views.add(img4);
		
		ImageView img5 = new ImageView(mActivity);
		img4.setBackgroundResource(R.drawable.zhuyeguanggao4);
		views.add(img5);

	}
	@Override
    public int getCount() {
        // TODO Auto-generated method stub
        return views.size();
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        // TODO Auto-generated method stub
        return arg0 == arg1;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // TODO Auto-generated method stub
        ((ViewPager) container).removeView(views.get(position));
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
//        ImageView imageView = new ImageView(activity);
    	ImageView imageView = new ImageView(mActivity);
		imageView.setBackgroundResource(position == 0 ? R.drawable.zhuyeguanggao1 :
										position == 1 ? R.drawable.zhuyeguanggao2 :
										position == 2 ? R.drawable.zhuyeguanggao3 :
										position == 3 ? R.drawable.zhuyeguanggao4 :
										R.drawable.zhuyeguanggao5);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setPadding(5, 0, 8, 0);
//        ImageLoader imageLoader = new ImageLoader(queue, imageCache);
//        ImageListener imageListener =
//                imageLoader.getImageListener(imageView, R.drawable.loading53, R.drawable.loading53);
//        imageLoader.get(mListviews.get(position), imageListener);

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // Toast.makeText(context, "广告位跳转", Toast.LENGTH_SHORT).show();

              if (position==getCount()-1){
                  Intent intent = new Intent(mActivity, HomeActivity.class);
                  mActivity.startActivity(intent);
                  mActivity.finish();

              }
            }
        });
        setGuided();
        container.addView(imageView);
        return imageView;
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemOnClickListener(OnItemClickListener listener) {
        mOnItemClickListener = listener;
    }

    public interface OnItemClickListener {
        public void onItemClickListener(int position);
    }
    /**
     *
     * method desc：设置已经引导过了，下次启动不用再次引导
     */
    private void setGuided() {
        SharedPreferences preferences = mActivity.getSharedPreferences(
                SHAREDPREFERENCES_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        // 存入数据
        editor.putBoolean("isFirstIn", false);
        // 提交修改
        editor.commit();
    }

}
